import connector.driver
from connector.driver import DatabaseDriver


# 用户注册
def register_user(driver: DatabaseDriver, username: str, password: str, role: int, without_id=True, id: int = None):
    # 从数据库判断是否有重名的用户
    result: tuple = driver.query_one(
        'select user_id from user where user_name = "{}"'.format(username), log=True)
    if result is None:
        if without_id:
            # 判断是否需要用户自定义id
            update: int = driver.insert(
                'insert into user(user_name,user_password,user_role) values ("{}","{}",{})'.format(username, password,
                                                                                                   role),
                log=True)
        else:
            if id is not None:
                update: int = driver.insert(
                    'insert into user values ({},"{}","{}",{})'.format(id, username, password,
                                                                       role),
                    log=True)
    else:
        return 0
    return update


# 用户登录
def login(driver: DatabaseDriver, username: str, password: str) -> tuple:
    # 根据用户名和密码查询是否有符合的用户
    result: tuple = driver.query_one(
        'select user_id,user_role from user where user_name = "{}" and user_password="{}"'.format(username, password),
        log=True)
    return result


# 查询系统当中所有的用户
def get_users(driver: DatabaseDriver) -> tuple:
    # 获取系统中所有的用户
    result: tuple = driver.query_all("select * from user")
    return result


# 更新用户数据
def update_user(driver: DatabaseDriver, user_id: int, username: str, password: str, role: int):
    # 更新用户
    result: int = driver.update(
        'update user set user_name="{}",user_password="{}",user_role={} where user_id={}'.format(username, password,
                                                                                                 role,
                                                                                                 user_id))
    return result


# 重置密码
def reset_password(driver: DatabaseDriver, user_id: int, new_password: str):
    # 更新用户
    result: int = driver.update(
        'update user set user_password="{}" where user_id={}'.format(new_password,
                                                                     user_id))
    return result


def get_user_by_id(driver: DatabaseDriver, user_id: int) -> tuple:
    result: tuple = driver.query_one("select * from user where user_id={}".format(user_id))
    return result


def get_user_by_name(driver: DatabaseDriver, user_name: str) -> bool:
    result: tuple = driver.query_one('select * from user where user_name="{}"'.format(user_name))
    if result is not None and len(result) == 1:
        return True
    return False


# 根据用户名和角色筛选
def get_user_by_username_or_role(driver: DatabaseDriver, username: str, role_id: int) -> list:
    if role_id != None and username != "":
        return driver.query_all('select * from user where user_name="{}" and user_role={}'.format(username, role_id))
    else:
        if role_id is None:
            return driver.query_all('select * from user where user_name like "%{}%"'.format(username))
        else:
            return driver.query_all(
                'select * from user where user_name like "%{}%" and user_role={}}'.format(username, role_id))

def delete_user(driver: DatabaseDriver, user_id: int)->int:
    return driver.delete("delete from user where user_id={}".format(user_id))




if __name__ == "__main__":
    driver = connector.driver.DatabaseDriver()

    # reset_password(driver,4,"hzkjzyjsxy")
    # update_user(driver,user_id=4,username="flv",password="123",role=0)
    # print(get_users(driver))
    print(get_user_by_id())
    driver.close()
